using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ProductManagement.Models;

namespace ProductManagement.Services
{
    public class ProductService : IProductService
    {
        private static List<Product> _products = new()
        {
            new Product { Id = 1, Name = "笔记本电脑", Description = "高性能办公笔记本", Price = 5999.99m, Stock = 10, Category = "电子产品" },
            new Product { Id = 2, Name = "无线鼠标", Description = "蓝牙无线鼠标", Price = 99.99m, Stock = 50, Category = "电子产品" },
            new Product { Id = 3, Name = "办公椅", Description = "人体工学办公椅", Price = 899.99m, Stock = 20, Category = "办公用品" },
            new Product { Id = 4, Name = "咖啡杯", Description = "陶瓷咖啡杯", Price = 29.99m, Stock = 100, Category = "生活用品" }
        };

        private static int _nextId = 5;

        public Task<List<Product>> GetAllProductsAsync()
        {
            return Task.FromResult(_products.ToList());
        }

        public Task<List<Product>> GetProductsByCategoryAsync(string category)
        {
            var products = string.IsNullOrEmpty(category) 
                ? _products.ToList()
                : _products.Where(p => p.Category.Equals(category, StringComparison.OrdinalIgnoreCase)).ToList();
            return Task.FromResult(products);
        }

        public Task<Product?> GetProductByIdAsync(int id)
        {
            var product = _products.FirstOrDefault(p => p.Id == id);
            return Task.FromResult(product);
        }

        public Task<bool> AddProductAsync(Product product)
        {
            try
            {
                product.Id = _nextId++;
                product.CreateDate = DateTime.Now;
                _products.Add(product);
                return Task.FromResult(true);
            }
            catch
            {
                return Task.FromResult(false);
            }
        }

        public Task<bool> UpdateProductAsync(Product product)
        {
            try
            {
                var existingProduct = _products.FirstOrDefault(p => p.Id == product.Id);
                if (existingProduct != null)
                {
                    existingProduct.Name = product.Name;
                    existingProduct.Description = product.Description;
                    existingProduct.Price = product.Price;
                    existingProduct.Stock = product.Stock;
                    existingProduct.Category = product.Category;
                    existingProduct.IsActive = product.IsActive;
                    return Task.FromResult(true);
                }
                return Task.FromResult(false);
            }
            catch
            {
                return Task.FromResult(false);
            }
        }

        public Task<bool> DeleteProductAsync(int id)
        {
            try
            {
                var product = _products.FirstOrDefault(p => p.Id == id);
                if (product != null)
                {
                    _products.Remove(product);
                    return Task.FromResult(true);
                }
                return Task.FromResult(false);
            }
            catch
            {
                return Task.FromResult(false);
            }
        }

        public Task<List<string>> GetCategoriesAsync()
        {
            var categories = _products.Select(p => p.Category).Distinct().ToList();
            return Task.FromResult(categories);
        }
    }
}